package com.yycx.module.pay.provider.strategy;

import cn.hutool.core.map.MapUtil;
import com.egzosn.pay.common.bean.PayMessage;
import com.egzosn.pay.common.bean.PayOrder;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.utils.SpringContextHolder;
import com.yycx.module.pay.client.entity.PayInfo;
import com.yycx.module.pay.client.enums.PayType;
import com.yycx.module.pay.client.enums.PayTypeEnum;
import com.yycx.module.pay.client.vo.PayRequest;
import com.yycx.module.pay.provider.handler.PaySuccessHandler;
import com.yycx.module.pay.provider.response.PayResponse;
import com.yycx.module.pay.provider.service.PayBalanceService;
import com.yycx.module.pay.provider.strategy.base.BasePayStrategy;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 余额支付参数获取实现
 *
 * @author zyf
 */
@Component
@AllArgsConstructor
public class BalancePayStrategy implements BasePayStrategy {


    private final PayBalanceService balanceService;

    @Override
    public boolean support(String payType, String transactionType) {
        if (PayTypeEnum.balance.name().equals(payType)) {
            return true;
        }
        return false;
    }

    @Override
    public ResultBody getPayResult(ResultBody resultBody, EntityMap result, PayResponse payResponse, PayOrder payOrder, String transactionType) {
        PayRequest payRequest = new PayRequest();
        PayInfo payInfo = (PayInfo) resultBody.getData();
        //支付密码
        String payPwd = MapUtil.getStr(result, "payPwd");
        //是否是扫码支付
        Object appScanPay = MapUtil.getStr(result, "appScanPay");
        Object handlerName = MapUtil.getStr(result, "handlerName");
        Object outTradeNo = MapUtil.getStr(result, "outTradeNo");
        payRequest.setUserId(payInfo.getUserId());
        payRequest.setUserType(payInfo.getUserType());
        payRequest.setPayAmount(payInfo.getOrderAmount());
        payRequest.setOutTradeNo(payInfo.getOutTradeNo());
        payRequest.setLogType(2);
        payRequest.setBusId(payInfo.getBusId());
        payRequest.setTitle(payInfo.getOrderTitle());
        payRequest.setBody(payInfo.getOrderBody());
        payRequest.setStatus(1);
        payRequest.setPayPwd(payPwd);
        payRequest.setUseBindAmount(true);
        payRequest.setUseBindAmountType(2);
        payRequest.setAppScanPay(appScanPay);
        boolean b = balanceService.subtractBalance(payRequest);
        if (!b) {
            return ResultBody.failed("支付失败");
        }
        PaySuccessHandler handler = SpringContextHolder.getHandler(handlerName.toString(), PaySuccessHandler.class);
        result.put("code", "SUCCESS");
        result.put("info", "支付成功");
        result.put("receipt_amount", payInfo.getOrderAmount() + "");
        result.put("busId", payInfo.getBusId());
        result.put("outTradeNo", outTradeNo);
        result.put("payType", PayType.balance.name());
        result.put("attach", payInfo.getAttach());
        PayMessage payMessage = new PayMessage(result);
        payMessage.setPayType(PayType.balance.name());
        handler.success(payMessage, payInfo.getOutTradeNo(), result, payResponse.getService());
        result.put("payInfo", payInfo);
        return resultBody;
    }
}